package com.hnichr.ping.sms.channel.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnichr.ping.common.DictEnum;
import com.hnichr.ping.common.vo.MobileResultVo;
import com.hnichr.ping.common.vo.SendResultVo;
import com.hnichr.ping.common.vo.SmsSendVo;
import com.hnichr.ping.sms.channel.SmsChannelProvider;
import com.hnichr.ping.sms.entity.SmsChannel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class DingXinSmsChannelProvider implements SmsChannelProvider {
    private static final Logger logger = LoggerFactory.getLogger(DingXinSmsChannelProvider.class);

    private static final String PROVIDER_CODE = "dingxin";

    @Override
    public String getProviderCode() {
        return PROVIDER_CODE ;
    }

    @Override
    public SendResultVo paramVerify(SmsSendVo smsSendVo){
        if(StringUtils.isBlank(smsSendVo.getSmsContent())){
            return SendResultVo.failed("smsContent不能为空");
        }
        return SendResultVo.success();
    }

    @Override
    public SendResultVo send(SmsChannel channel, SmsSendVo smsSendVo) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("AppID", channel.getPrincipal());
        paramMap.put("AppSecret",channel.getIdentity());
        paramMap.put("aimcodes", smsSendVo.getMobiles());
        if(StringUtils.isNotBlank(channel.getPerfix())){
            paramMap.put("content", smsSendVo.getSmsContent()+"【"+channel.getPerfix()+"】");
        }
        else{
            paramMap.put("content", smsSendVo.getSmsContent());
        }
        paramMap.put("dataType","json");

        try{
            String postReturn = HttpUtil.post(channel.getUrl(),paramMap);
            if(StringUtils.isBlank(postReturn)){
                return SendResultVo.failed("发送失败");
            }
            JSONObject returnObject = JSON.parseObject(postReturn);
            if(returnObject==null){
                return SendResultVo.failed(postReturn);
            }
            String replyCode = returnObject.getString("replyCode");
            if(StringUtils.equals(replyCode,"1")){
                SendResultVo sendResultVo = SendResultVo.success();
                for(String mobile:smsSendVo.getMobiles().split(",")){
                    MobileResultVo vo = new MobileResultVo();
                    vo.setMobile(mobile);
                    vo.setResultCode(DictEnum.SEND_RESULT.OK.getCode());
                    sendResultVo.getResult().add(vo);
                }
                return sendResultVo;
            }
            return SendResultVo.failed("发送失败");

        }
        catch (Exception e) {
            logger.error("发送短信失败", e);
            return SendResultVo.failed(e.getMessage());
        }

    }

    public static void main(String [] arg){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("AppID", "FhU8Eb4d");
        paramMap.put("AppSecret","4cb44a713e6e92c95a289487ef297f6d5fb37c99");
        paramMap.put("aimcodes", "18637109325,17888843296");
        paramMap.put("content", "验证码：999666，5分钟内有效。欢迎登录黄河人才网！【黄河人才网】");
        paramMap.put("dataType","json");
        String postReturn = HttpUtil.post("http://sdk.lx198.com/sdk/send",paramMap);
        System.out.println("postReturn>>>>>>>>>"+postReturn);
    }
}
